package windows;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;
import java.awt.Font;

import main.ImageLibrary;
import main.Telecommando;

import base.*;

public class TVRemote extends Window {

	private Vector<Button> buttons = new Vector<Button>( );
	private Vector<Button> buttonsNum = new Vector<Button>( );

	// Window and button variables
	private Button pressed;
	private boolean butPressed = false;
	private boolean viewNumPad = false;
	private int lastX;
	private int lastY;

	// TV Status buttons
	private static enum tvStatus{    TV_OFF, TV_ON  };
	private static enum inputStatus{ CABLE, VIDEO, DVD, BLUERAY };
	private static enum muteStatus{  MUTE_OFF, MUTE_ON };
	private static enum guideStatus{ GUIDE_OFF, GUIDE_ON };
	private static enum menuStatus{  MENU_OFF, MENU_ON };
	
	private tvStatus    tvCurStatus    = tvStatus.TV_ON;
	private inputStatus inputCurStatus = inputStatus.CABLE;
	private muteStatus  muteCurStatus  = muteStatus.MUTE_OFF;
	private guideStatus guideCurStatus = guideStatus.GUIDE_OFF;
	private menuStatus  menuCurStatus  = menuStatus.MENU_OFF;
	
	private int channel = 0;
	private int lastChannel = 0;
	private int volume = 10;
	private int curBuildChannel = 0;
	
	
	private final int MAX_CHANNELS = 9999;
	private final int MAX_VOLUME = 20;
	
	
	
	public TVRemote ( )
	{
		// Rooms:  Bedroom, Living Room, Kitchen
		super( "TV Remote" , "Favorites" , "Bedroom[0]" , "Uncategorized" );
		
		/*// Model 1
		Point mainRowStart = new Point(13, 3);
		Point guideBlockStart = new Point(13, 42);
		Point numBlockStart = new Point(18, 159);
		
		// Set up points for main row
		Point menuPos  = new Point( mainRowStart.x + 0,  mainRowStart.y + 0 );
		Point powerPos = new Point( mainRowStart.x + 59, mainRowStart.y + 0 );
		Point inputPos = new Point( mainRowStart.x + 104, mainRowStart.y + 0 );
		
		// Set up points for guide block
		Point guidePos = new Point( guideBlockStart.x + 0,  guideBlockStart.y + 0);
		Point chUPPos  = new Point( guideBlockStart.x + 52, guideBlockStart.y + 0);
		Point escPos   = new Point( guideBlockStart.x + 104, guideBlockStart.y + 0);
		Point volDNPos = new Point( guideBlockStart.x + 0,  guideBlockStart.y + 39);
		Point okPos    = new Point( guideBlockStart.x + 52, guideBlockStart.y + 39);
		Point volUPPos = new Point( guideBlockStart.x + 104, guideBlockStart.y + 39);
		Point lastPos  = new Point( guideBlockStart.x + 0,  guideBlockStart.y + 78);
		Point chDNPos  = new Point( guideBlockStart.x + 52, guideBlockStart.y + 78);
		Point mutePos  = new Point( guideBlockStart.x + 104, guideBlockStart.y + 78);
		
		// Set up points for number block
		Point zeroPos  = new Point( numBlockStart.x + 104, numBlockStart.y + 0);
		Point onePos   = new Point( numBlockStart.x + 0,  numBlockStart.y + 39);
		Point twoPos   = new Point( numBlockStart.x + 52, numBlockStart.y + 39);
		Point threePos = new Point( numBlockStart.x + 104, numBlockStart.y + 39);
		Point fourPos  = new Point( numBlockStart.x + 0,  numBlockStart.y + 78);
		Point fivePos  = new Point( numBlockStart.x + 52, numBlockStart.y + 78);
		Point sixPos   = new Point( numBlockStart.x + 104, numBlockStart.y + 78);
		Point sevenPos = new Point( numBlockStart.x + 0,  numBlockStart.y + 117);
		Point eightPos = new Point( numBlockStart.x + 52, numBlockStart.y + 117);
		Point ninePos  = new Point( numBlockStart.x + 104, numBlockStart.y + 117);
		
		// Add the buttons
		buttons.add( new Button( "menu_button",  menuPos,  "Images/TVRemote/Model_1/button_menu.png",      false) );
		buttons.add( new Button( "power_button", powerPos, "Images/TVRemote/Model_1/button_power.png",     false) );
		buttons.add( new Button( "input_button", inputPos, "Images/TVRemote/Model_1/button_input.png",     false) );
		buttons.add( new Button( "guide_button", guidePos, "Images/TVRemote/Model_1/button_guide.png",     false) );
		buttons.add( new Button( "chUP_button",  chUPPos,  "Images/TVRemote/Model_1/button_ch_plus.png",   false) );
		buttons.add( new Button( "esc_button",   escPos,   "Images/TVRemote/Model_1/button_esc.png",       false) );
		buttons.add( new Button( "volDN_button", volDNPos, "Images/TVRemote/Model_1/button_vol_minus.png", false) );
		buttons.add( new Button( "ok_button",    okPos,    "Images/TVRemote/Model_1/button_ok.png",        false) );
		buttons.add( new Button( "volUP_button", volUPPos, "Images/TVRemote/Model_1/button_vol_plus.png",  false) );
		buttons.add( new Button( "last_button",  lastPos,  "Images/TVRemote/Model_1/button_last.png",      false) );
		buttons.add( new Button( "chDN_button",  chDNPos,  "Images/TVRemote/Model_1/button_ch_minus.png",  false) );
		buttons.add( new Button( "mute_button",  mutePos,  "Images/TVRemote/Model_1/button_mute.png",      false) );
		buttons.add( new Button( "zero_button",  zeroPos,  "Images/TVRemote/Model_1/button_0.png",         false) );
		buttons.add( new Button( "one_button",   onePos,   "Images/TVRemote/Model_1/button_1.png",         false) );
		buttons.add( new Button( "two_button",   twoPos,   "Images/TVRemote/Model_1/button_2.png",         false) );
		buttons.add( new Button( "three_button", threePos, "Images/TVRemote/Model_1/button_3.png",         false) );
		buttons.add( new Button( "four_button",  fourPos,  "Images/TVRemote/Model_1/button_4.png",         false) );
		buttons.add( new Button( "five_button",  fivePos,  "Images/TVRemote/Model_1/button_5.png",         false) );
		buttons.add( new Button( "six_button",   sixPos,   "Images/TVRemote/Model_1/button_6.png",         false) );
		buttons.add( new Button( "seven_button", sevenPos, "Images/TVRemote/Model_1/button_7.png",         false) );
		buttons.add( new Button( "eight_button", eightPos, "Images/TVRemote/Model_1/button_8.png",         false) );
		buttons.add( new Button( "nine_button",  ninePos,  "Images/TVRemote/Model_1/button_9.png",         false) );*/	
		
		
		// -=-=-=-=-=-=-=-=-=- Model 2 Of Remote -=-=-=-=-=-=-=-=-=-
		Point topRowStart     = new Point(8, 5);
		Point guideBlockStart = new Point(5, 55);
		Point bottomRowStart  = new Point(8, 225);
		
		// Set up points for the top row
		Point powerPos = new Point( topRowStart.x + 0,   topRowStart.y + 0 );
		Point inputPos = new Point( topRowStart.x + 47,  topRowStart.y + 0 );
		Point mutePos  = new Point( topRowStart.x + 104, topRowStart.y + 0 );
		
		// Set up points for the guideBlock
		Point chUPPos  = new Point( guideBlockStart.x + 50,  guideBlockStart.y + 0 );
		Point volDNPos = new Point( guideBlockStart.x + 0,   guideBlockStart.y + 50 );
		Point okPos    = new Point( guideBlockStart.x + 57,  guideBlockStart.y + 57 );
		Point volUPPos = new Point( guideBlockStart.x + 100, guideBlockStart.y + 50 );
		Point chDNPos  = new Point( guideBlockStart.x + 50,  guideBlockStart.y + 100 );
		
		// Set up points for the bottom rows
		Point guidePos = new Point( bottomRowStart.x + 0,   bottomRowStart.y + 0 );
		Point escPos   = new Point( bottomRowStart.x + 52,  bottomRowStart.y + 0 );
		Point menuPos  = new Point( bottomRowStart.x + 104, bottomRowStart.y + 0 );
		Point chanPos  = new Point( bottomRowStart.x + 26,  bottomRowStart.y + 40 );
		Point lastPos  = new Point( bottomRowStart.x + 78,  bottomRowStart.y + 40 );
		
		buttons.add( new Button( "power_button", powerPos, "Images/TVRemote/Model_2/button_power.png", false, true ) );
		buttons.add( new Button( "input_button", inputPos, "Images/TVRemote/Model_2/button_input.png", false, true ) );
		buttons.add( new Button( "mute_button",  mutePos,  "Images/TVRemote/Model_2/button_mute.png",  false, true ) );
		buttons.add( new Button( "chUP_button",  chUPPos,  "Images/TVRemote/Model_2/button_chUp.png",  false, true ) );
		buttons.add( new Button( "volDN_button", volDNPos, "Images/TVRemote/Model_2/button_volDN.png", false, true ) );
		buttons.add( new Button( "ok_button",    okPos,    "Images/TVRemote/Model_2/button_ok.png",    false, true ) );
		buttons.add( new Button( "volUP_button", volUPPos, "Images/TVRemote/Model_2/button_volUP.png", false, true ) );
		buttons.add( new Button( "chDN_button",  chDNPos,  "Images/TVRemote/Model_2/button_chDN.png",  false, true ) );
		buttons.add( new Button( "guide_button", guidePos, "Images/TVRemote/Model_2/button_guide.png", false, true ) );
		buttons.add( new Button( "esc_button",   escPos,   "Images/TVRemote/Model_2/button_esc.png",   false, true ) );
		buttons.add( new Button( "menu_button",  menuPos,  "Images/TVRemote/Model_2/button_menu.png",  false, true ) );
		buttons.add( new Button( "chan_button",  chanPos,  "Images/TVRemote/Model_2/button_chan.png",  false, true ) );
		buttons.add( new Button( "last_button",  lastPos,  "Images/TVRemote/Model_2/button_last.png",  false, true ) );
		
		// -=-=-=-=-=-=-=-=-=- Model 1 Of Number Pad -=-=-=-=-=-=-=-=-=-

		Point numPadStart = new Point( 6, 62);
		
		ImageLibrary.add( "number_border", "Images/TVRemote/Model_2/number_border.png" );
		
		// Set up points for number pad
		Point onePos   = new Point( numPadStart.x + 0,   numPadStart.y + 0);
		Point twoPos   = new Point( numPadStart.x + 51,  numPadStart.y + 0);
		Point threePos = new Point( numPadStart.x + 102, numPadStart.y + 0);
		Point fourPos  = new Point( numPadStart.x + 0,   numPadStart.y + 51);
		Point fivePos  = new Point( numPadStart.x + 51,  numPadStart.y + 51);
		Point sixPos   = new Point( numPadStart.x + 102, numPadStart.y + 51);
		Point sevenPos = new Point( numPadStart.x + 0,   numPadStart.y + 102);
		Point eightPos = new Point( numPadStart.x + 51,  numPadStart.y + 102);
		Point ninePos  = new Point( numPadStart.x + 102, numPadStart.y + 102);
		Point backPos  = new Point( numPadStart.x + 0,   numPadStart.y + 153);
		Point zeroPos  = new Point( numPadStart.x + 51,  numPadStart.y + 153);
		Point enterPos = new Point( numPadStart.x + 102, numPadStart.y + 153);
		
		buttonsNum.add( new Button( "one_button",   onePos,   "Images/TVRemote/Model_2/button_1.png",         false, true ) );
		buttonsNum.add( new Button( "two_button",   twoPos,   "Images/TVRemote/Model_2/button_2.png",         false, true ) );
		buttonsNum.add( new Button( "three_button", threePos, "Images/TVRemote/Model_2/button_3.png",         false, true ) );
		buttonsNum.add( new Button( "four_button",  fourPos,  "Images/TVRemote/Model_2/button_4.png",         false, true ) );
		buttonsNum.add( new Button( "five_button",  fivePos,  "Images/TVRemote/Model_2/button_5.png",         false, true ) );
		buttonsNum.add( new Button( "six_button",   sixPos,   "Images/TVRemote/Model_2/button_6.png",         false, true ) );
		buttonsNum.add( new Button( "seven_button", sevenPos, "Images/TVRemote/Model_2/button_7.png",         false, true ) );
		buttonsNum.add( new Button( "eight_button", eightPos, "Images/TVRemote/Model_2/button_8.png",         false, true ) );
		buttonsNum.add( new Button( "nine_button",  ninePos,  "Images/TVRemote/Model_2/button_9.png",         false, true ) );
		buttonsNum.add( new Button( "back_button",  backPos,  "Images/TVRemote/Model_2/button_num_back.png",  false, true ) );
		buttonsNum.add( new Button( "zero_button",  zeroPos,  "Images/TVRemote/Model_2/button_0.png",         false, true ) );
		buttonsNum.add( new Button( "enter_button", enterPos, "Images/TVRemote/Model_2/button_num_enter.png", false, true ) );
		
		System.out.println( "Current TV status set to ON" );
	}
	
	@Override
	public void draw(Graphics2D g2d) {
		
		if ( viewNumPad ) {
			for ( Button but : buttonsNum ) {
				but.draw( g2d );
			}
			
			
			
			// Writing Number
			int fontSize = 42;
			Point labelStart  = new Point( 12, 48 );
			Point borderStart = new Point( 6, 6 );
			g2d.setColor(Color.WHITE);
			Font numFont  = new Font("NumFont", Font.BOLD, fontSize );
			g2d.setFont(numFont);
			String text = "" + curBuildChannel;
			g2d.drawString( text, labelStart.x, labelStart.y );
			g2d.drawImage( ImageLibrary.get("number_border"), borderStart.x, borderStart.y, Telecommando.getInstance( ) );
		}
		else 
			for( Button but : buttons )
				but.draw( g2d );
		
		
	}

	@Override
	public void fingerPressed(Point location) {
		
		lastX = location.x;
		lastY = location.y;
		if ( viewNumPad ) {
			for( Button but : buttonsNum ) {
				if( but.press( location.x , location.y ) ) {
					pressed = but;
					butPressed = true;
				}
			}	
		}
		else {
			for( Button but : buttons ) {
				if( but.press( location.x , location.y ) ) {
					pressed = but;
					butPressed = true;
				}
			}
		}
	}

	@Override
	public void fingerReleased(Point location) {
		
		boolean skipAction = false;
		
		// this if section will tell this function to not perform button actions if a button
		// was pressed but released off of that button
		if ( butPressed ) {
			if ( !pressed.getRect( ).contains(location) ) { 
				//The button released in is outside of pressed button; don't do button call
				skipAction = true;
				pressed.release();
			}
		}
		
		if ( viewNumPad && !skipAction) {
			for( Button but : buttonsNum ) {
				if( but.isPressed( ) ) {
					
					String butName = but.getName( );
					int pushedNumber = 0;
					boolean skipBuildNewChannel = false;
					
					if ( butName.equals("one_button") ) 
						pushedNumber = 1;
					else if ( butName.equals("two_button") ) 
						pushedNumber = 2;
					else if ( butName.equals("three_button") ) 
						pushedNumber = 3;
					else if ( butName.equals("four_button") ) 
						pushedNumber = 4;
					else if ( butName.equals("five_button") ) 
						pushedNumber = 5;
					else if ( butName.equals("six_button") ) 
						pushedNumber = 6;
					else if ( butName.equals("seven_button") ) 
						pushedNumber = 7;
					else if ( butName.equals("eight_button") ) 
						pushedNumber = 8;
					else if ( butName.equals("nine_button") ) 
						pushedNumber = 9;
					else if ( butName.equals("zero_button") ) 
						pushedNumber = 0;
					else if ( butName.equals("back_button") ) {
						if (curBuildChannel != 0) {
							curBuildChannel = curBuildChannel / 10;
							skipBuildNewChannel = true;
							}
					}
					else if ( butName.equals("enter_button") ) {
						channel = curBuildChannel;
						System.out.println("Channel set to " + channel );
						curBuildChannel = 0;
						skipBuildNewChannel = true;
						viewNumPad = false;
					}
					
					if ( !skipBuildNewChannel ) {
						int newBuildChannel = 10 * curBuildChannel + pushedNumber;
						curBuildChannel = newBuildChannel;
						
						if ( curBuildChannel >= 1000 && curBuildChannel < 10000 ) {
							channel = curBuildChannel;
							System.out.println("Channel set to " + channel );
							curBuildChannel = 0;
							viewNumPad = false;
						}
					}
					
					but.release( );
				}
			}
		}
		else if ( !viewNumPad && !skipAction) {
			for( Button but : buttons ) {
				if( but.isPressed( ) ) {
					String butName = but.getName( );
					

					
					if ( butName.equals("power_button") ) {
						switch(tvCurStatus) {
							case TV_OFF: 
								tvCurStatus = tvStatus.TV_ON;
								System.out.println( "Current TV status set to ON" );
								break;
							case TV_ON:
								tvCurStatus = tvStatus.TV_OFF;
								System.out.println( "Current TV status set to OFF" );
								break;
							default:
								System.out.println( "TV button pressed, no case!?" );							
						}
					}
					else if ( butName.equals("input_button") ) {
						
						if ( tvCurStatus.equals(tvStatus.TV_ON) && 
						     guideCurStatus.equals(guideStatus.GUIDE_OFF) && 
						     menuCurStatus.equals(menuStatus.MENU_OFF) ) 
						{
							switch(inputCurStatus) {
								case CABLE:
									inputCurStatus = inputStatus.VIDEO;
									System.out.println( "Current INPUT status set to VIDEO" );
									break;
								case VIDEO:
									inputCurStatus = inputStatus.DVD;
									System.out.println( "Current INPUT status set to DVD" );
									break;
								case DVD:
									inputCurStatus = inputStatus.BLUERAY;
									System.out.println( "Current INPUT status set to BLUERAY" );
									break;
								case BLUERAY:
									inputCurStatus = inputStatus.CABLE;
									System.out.println( "Current INPUT status set to CABLE" );
									break;
								default:
									System.out.println( "INPUT button pressed, no case!?");
							}
						}
					}
					else if ( butName.equals("mute_button") ) {
						
						if ( tvCurStatus.equals(tvStatus.TV_ON) && 
							 menuCurStatus.equals(menuStatus.MENU_OFF) ) 
						{
							switch(muteCurStatus) {
								case MUTE_OFF: 
									muteCurStatus = muteStatus.MUTE_ON;
									System.out.println( "Current MUTE status set to ON" );
									break;
								case MUTE_ON:
									muteCurStatus = muteStatus.MUTE_OFF;
									System.out.println( "Current MUTE status set to OFF" );
									break;
								default:
									System.out.println( "MUTE button pressed, no case!?" );							
							}
						}
						
					}
					else if ( butName.equals("chUP_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( !inputCurStatus.equals(inputStatus.CABLE) ) {
								System.out.println("Do current INPUT function for up button" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
								 menuCurStatus.equals(menuStatus.MENU_OFF ) )
							{
								if( channel < MAX_CHANNELS) {
									lastChannel = channel;
									channel++;
								}
								else {
									lastChannel = channel;
									channel = 0;
								}
								System.out.println( "Channel increased to " + channel );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
									  menuCurStatus.equals(menuStatus.MENU_ON) )
							{
								System.out.println( "Menu cursor while guide is on moved up" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								System.out.println( "Guide cursor moved up");
							}
							else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								System.out.println( "Menu cursor moved up" );
							}
						}
					}
					else if ( butName.equals("volDN_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
								 menuCurStatus.equals(menuStatus.MENU_OFF ) )
							{
								if( volume > 0 ) {
									volume--;
								}
								
								System.out.println( "Volume decreased to " + volume );
								
								if ( muteCurStatus.equals(muteStatus.MUTE_ON) ) {
									muteCurStatus = muteStatus.MUTE_OFF;
									System.out.println( "Mute canceled" );
								}
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
									  menuCurStatus.equals(menuStatus.MENU_ON) )
							{
								System.out.println( "Menu cursor while guide is on moved left" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								System.out.println( "Guide cursor moved left");
							}
							else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								System.out.println( "Menu cursor moved left" );
							}
						}
					}
					else if ( butName.equals("ok_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								menuCurStatus = menuStatus.MENU_OFF;
								System.out.println( "Menu option /entered if another menu, selected if option/" );
								System.out.println( "(selected option assumed)Current MENU status set to OFF" );		
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								guideCurStatus = guideStatus.GUIDE_OFF;
								System.out.println( "Guide option /entered if another menu, selected if option/" );
								System.out.println( "(selected option assumed) Current GUIDE status set to OFF" );		
							}
						}
					}
					else if ( butName.equals("volUP_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
								 menuCurStatus.equals(menuStatus.MENU_OFF ) )
							{
								if( volume < MAX_VOLUME ) {
									volume++;
								}
								
								System.out.println( "Volume increased to " + volume );
								
								if ( muteCurStatus.equals(muteStatus.MUTE_ON) ) {
									muteCurStatus = muteStatus.MUTE_OFF;
									System.out.println( "Mute canceled" );
								}
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
									  menuCurStatus.equals(menuStatus.MENU_ON) )
							{
								System.out.println( "Menu cursor while guide is on moved right" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								System.out.println( "Guide cursor moved right");
							}
							else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								System.out.println( "Menu cursor moved right" );
							}
						}
					}
					else if ( butName.equals("chDN_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( !inputCurStatus.equals(inputStatus.CABLE) ) {
								System.out.println("Do current INPUT function for up button" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
								 menuCurStatus.equals(menuStatus.MENU_OFF ) )
							{
								if( channel > 0 ) {
									lastChannel = channel;
									channel--;
								}
								else {
									lastChannel = channel;
									channel = 9999;
								}
								System.out.println( "Channel decreased to " + channel );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
									  menuCurStatus.equals(menuStatus.MENU_ON) )
							{
								System.out.println( "Menu cursor while guide is on moved down" );
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								System.out.println( "Guide cursor moved down");
							}
							else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								System.out.println( "Menu cursor moved down" );
							}
						}
					}
					else if ( butName.equals("guide_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) && 
						     menuCurStatus.equals(menuStatus.MENU_OFF) && 
						     !inputCurStatus.equals(inputStatus.CABLE) ) 
						{
							switch(guideCurStatus) {
								case GUIDE_OFF: 
									guideCurStatus = guideStatus.GUIDE_ON;
									System.out.println( "Current GUIDE status set to ON" );
									break;
								case GUIDE_ON:
									guideCurStatus = guideStatus.GUIDE_OFF;
									System.out.println( "Current GUIDE status set to OFF" );
									break;
								default:
									System.out.println( "GUIDE button pressed, no case!?" );							
							}
						}
					}
					else if ( butName.equals("esc_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
								menuCurStatus = menuStatus.MENU_OFF;
								System.out.println( "Current MENU status set to OFF" );	
							}
							else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
								guideCurStatus = guideStatus.GUIDE_OFF;
								System.out.println( "Current GUIDE status set to OFF" );		
							}
						}
					}
					else if ( butName.equals("menu_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
							switch(menuCurStatus) {
								case MENU_OFF: 
									menuCurStatus = menuStatus.MENU_ON;
									System.out.println( "Current MENU status set to ON" );
									break;
								case MENU_ON:
									menuCurStatus = menuStatus.MENU_OFF;
									System.out.println( "Current MENU status set to OFF" );
									break;
								default:
									System.out.println( "MENU button pressed, no case!?" );							
							}
						}
					}
					else if ( butName.equals("chan_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) && 
							 menuCurStatus.equals(menuStatus.MENU_OFF)) 
						{
							viewNumPad = true;
						}
						
					}
					else if ( butName.equals("last_button") ) {
						if ( tvCurStatus.equals(tvStatus.TV_ON) && 
							 menuCurStatus.equals(menuStatus.MENU_OFF) &&
							 !inputCurStatus.equals(inputStatus.CABLE)) 
						{
							int temp = channel;
							channel = lastChannel;
							lastChannel = temp;
							System.out.println( "Channel set to " + channel );
						}
					}
					
					but.release( );
				}
			}
		}		
		
		// scroll up
		if ( ( lastY - location.y ) >= 30 && !viewNumPad ) {
			// TODO copied, consider function call
			if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
				if ( !inputCurStatus.equals(inputStatus.CABLE) ) {
					System.out.println("Do current INPUT function for up button" );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
					 menuCurStatus.equals(menuStatus.MENU_OFF ) )
				{
					if( channel < MAX_CHANNELS) {
						lastChannel = channel;
						channel++;
					}
					else {
						lastChannel = channel;
						channel = 0;
					}
					System.out.println( "Channel increased to " + channel );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
						  menuCurStatus.equals(menuStatus.MENU_ON) )
				{
					System.out.println( "Menu cursor while guide is on moved up" );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
					System.out.println( "Guide cursor moved up");
				}
				else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
					System.out.println( "Menu cursor moved up" );
				}
			}
		}
		else if ( ( lastY - location.y ) <= -30 && !viewNumPad ) {
			// TODO copied, consider function call
			if ( tvCurStatus.equals(tvStatus.TV_ON) ) {
				if ( !inputCurStatus.equals(inputStatus.CABLE) ) {
					System.out.println("Do current INPUT function for up button" );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_OFF) &&
					 menuCurStatus.equals(menuStatus.MENU_OFF ) )
				{
					if( channel > 0 ) {
						lastChannel = channel;
						channel--;
					}
					else {
						lastChannel = channel;
						channel = 9999;
					}
					System.out.println( "Channel decreased to " + channel );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) &&
						  menuCurStatus.equals(menuStatus.MENU_ON) )
				{
					System.out.println( "Menu cursor while guide is on moved down" );
				}
				else if ( guideCurStatus.equals(guideStatus.GUIDE_ON) ) {
					System.out.println( "Guide cursor moved down");
				}
				else if ( menuCurStatus.equals(menuStatus.MENU_ON) ) {
					System.out.println( "Menu cursor moved down" );
				}
			}
		}
		else if( ( ( lastX - location.x ) <= -30 || (lastX - location.x ) >= 30 ) && 
				 viewNumPad && tvCurStatus.equals(tvStatus.TV_ON) ) 
			viewNumPad = false;
		else if ( ( ( lastX - location.x ) <= -30 || (lastX - location.x ) >= 30 ) && 
				  !viewNumPad && tvCurStatus.equals(tvStatus.TV_ON) ) 
			viewNumPad = true;		
		
		// Because all buttons are released, no button could be pressed right now
		pressed = null;
		butPressed = false;
	}

	@Override
	public void gestureHorizontal(int amount) {
		
	}

	@Override
	public void gestureVertical(int amount) {
		
	}

}
